﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FGrubert.CrmQueueMonitor.WinForms
{
    /// <summary>
    /// A comparer for the default order of the queue config (first type, then name)
    /// </summary>
    public class QueueConfigDefaultComparer : IComparer<QueueConfig>, IComparer
    {
        /// <summary>
        /// Stores the string comparer to use in case of the queue type
        /// </summary>
        private StringComparer _stringComparer = StringComparer.InvariantCultureIgnoreCase;

        /// <summary>
        /// Compares two queue configs
        /// </summary>
        /// <param name="x">The first element</param>
        /// <param name="y">The second element</param>
        /// <returns>A value less than 0 if y should be before x, 0 if both are equal, a value higher than 0 if y should be after x</returns>
        public int Compare(QueueConfig x, QueueConfig y)
        {
            if (x.QueueType == y.QueueType)
            {
                return _stringComparer.Compare(x.Name, y.Name);
            }
            else
            {
                return (int)x.QueueType - (int)y.QueueType;
            }
        }

        /// <summary>
        /// Compares two queue configs
        /// </summary>
        /// <param name="x">The first element (must be QueueConfig)</param>
        /// <param name="y">The second element (must be QueueConfig)</param>
        /// <returns>A value less than 0 if y should be before x, 0 if both are equal, a value higher than 0 if y should be after x</returns>
        public int Compare(object x, object y)
        {
            if (x is QueueConfig && y is QueueConfig)
            {
                return this.Compare((QueueConfig)x, (QueueConfig)y);
            }
            else
            {
                throw new ArgumentException("Comparation of " + x.GetType().Name + " and " + y.GetType().Name + " is not supported");
            }
        }
    }
}
